package com.smart.oauth2.entity;

import com.alibaba.fastjson.JSON;
import lombok.Getter;
import lombok.Setter;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.oauth2.provider.ClientDetails;

import java.util.*;

/**
 * @author zhangwei
 */
@Setter
@Getter
public class OauthClientDetails implements ClientDetails {
    private String clientId;

    private String autoApprove;
    /**
     * 重定向的url
     */
    private String webServerRedirectUri;
    /**
     * 令牌的有效期
     */
    private int accessTokenValidity;
    /**
     * 刷新令牌的时间
     */
    private int refreshTokenValidity;


    @Override
    public String getClientId() {
        return clientId;
    }


    private String clientSecret;

    @Override
    public String getClientSecret() {
        return clientSecret;
    }


    @Override
    public boolean isSecretRequired() {
        return clientSecret != null;
    }


    private String resourceIds;

    @Override
    public Set<String> getResourceIds() {
        if (resourceIds == null || resourceIds.trim().length() <= 0) {
            return Collections.emptySet();
        }
        String[] resources = resourceIds.split(",");
        return new LinkedHashSet<>(Arrays.asList(resources));
    }


    private String scope;

    @Override
    public Set<String> getScope() {
        if (scope == null || scope.trim().length() <= 0) {
            return Collections.emptySet();
        }
        String[] scopes = scope.split(",");
        return new LinkedHashSet<>(Arrays.asList(scopes));
    }


    @Override
    public boolean isScoped() {
        return scope != null && scope.trim().length() > 0;
    }


    private String authorizedGrantTypes;

    @Override
    public Set<String> getAuthorizedGrantTypes() {
        if (authorizedGrantTypes == null || authorizedGrantTypes.trim().length() <= 0) {
            return Collections.emptySet();
        }
        String[] types = authorizedGrantTypes.split(",");
        return new LinkedHashSet<>(Arrays.asList(types));
    }


    private String registeredRedirectUris;

    @Override
    public Set<String> getRegisteredRedirectUri() {
        if (registeredRedirectUris == null || registeredRedirectUris.trim().length() <= 0) {
            return Collections.emptySet();
        }
        String[] uris = registeredRedirectUris.split(",");
        return new LinkedHashSet<>(Arrays.asList(uris));
    }

    private String authorities;

    @Override
    public Collection<GrantedAuthority> getAuthorities() {
        if (authorities == null || authorities.trim().length() <= 0) {
            return Collections.emptyList();
        }
        List<GrantedAuthority> authorityList = new ArrayList<>();
        String[] authoritiesArray = authorities.split(",");
        for (String authorities : authoritiesArray) {
            authorityList.add((GrantedAuthority) () -> authorities);
        }
        return authorityList;
    }


    private Integer accessTokenValiditySeconds;

    @Override
    public Integer getAccessTokenValiditySeconds() {
        return accessTokenValiditySeconds;
    }


    private Integer refreshTokenValiditySeconds;

    @Override
    public Integer getRefreshTokenValiditySeconds() {
        return refreshTokenValiditySeconds;
    }


    private String additionalInformation;

    @Override
    public Map<String, Object> getAdditionalInformation() {
        if (additionalInformation == null || additionalInformation.trim().length() <= 0) {
            return Collections.emptyMap();
        }
        return JSON.parseObject(additionalInformation);
    }

    private String autoApproveScopes;

    public Set<String> getAutoApproveScopes() {
        if (autoApproveScopes == null || autoApproveScopes.trim().length() <= 0) {
            return Collections.emptySet();
        }
        String[] approves = autoApproveScopes.split(",");
        return new HashSet<>(Arrays.asList(approves));
    }

    @Override
    public boolean isAutoApprove(String scope) {
        if (getAutoApproveScopes() == null || getAutoApproveScopes().isEmpty()) {
            return false;
        } else {
            Iterator<String> var2 = getAutoApproveScopes().iterator();
            String auto;
            do {
                if (!var2.hasNext()) {
                    return false;
                }
                auto = var2.next();
            } while (!"true".equals(auto) && !scope.matches(auto));
            return true;
        }
    }

}